////
//
/// \file ./src/type_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/type_node.hpp>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/atomic_type_node.hpp>
#include <inc/parser/vector_type_node.hpp>

namespace inc {
namespace parser {

using boost::scoped_ptr;

/// \brief Basic constructor for a type node
/// \author Chandler Carruth
/// \date 2008.10.04

TypeNode::TypeNode()
{}

/// \brief Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date 2008.10.04

TypeNode::~TypeNode()
{}

/// \brief Parse a type in INC
/// \author Chandler Carruth
/// \date 2008.10.04
///
/// Parse a type specification in INC. This can be an atomic type, a vector
/// type, or in the future a pointer type.

bool
TypeNode::parse( const iterator &first,
                 const iterator &last,
                 iterator &last_parsed )
{
  assert( get_text().empty() && !node
          && "This node was parsed more than once!" );

  //  Try an integer type first.
  scoped_ptr<ParseTreeNode> atomic_type( new AtomicTypeNode );
  if( atomic_type->parse( first, last, last_parsed ) )
  {
    set_text( atomic_type->get_text() );
    node.swap( atomic_type );
    return true;
  }

  //  Now try a floating point type.
  scoped_ptr<ParseTreeNode> vector_type( new VectorTypeNode );
  if( vector_type->parse( first, last, last_parsed ) )
  {
    set_text( vector_type->get_text() );
    node.swap( vector_type );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief Convenience getter for an atomic type
/// \author Chandler Carruth
/// \date 2008.10.04

AtomicTypeNode* TypeNode::get_atomic_type() const
{
  return dynamic_cast<AtomicTypeNode*>( node.get() );
}

/// \brief Convenience getter for a vector type
/// \author Chandler Carruth
/// \date 2008.10.04

VectorTypeNode* TypeNode::get_vector_type() const
{
  return dynamic_cast<VectorTypeNode*>( node.get() );
}

} //  end parser namespace
} //  end inc namespace
